<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin_Payments extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->is_login(2);
        
        $this->load->library('pagination');

        $this->load->model('admin_estate_model');
        $this->load->model('settings_m');
        $this->load->model('payments_m');
        
        $this->load->helper('language');
        
        $this->lang->load('admin_estate');
        $this->lang->load('fx');
    }

    public function index() {
        $base_where = array();
        
        //$this->admin_estate_model->paginationLimit(10, $this->uri->segment(3));
        //$this->data['pagination'] = $this->paginationCreate('/admin_payments/index/', $this->payments_m->count_by($base_where), 10);

        $this->data['ptitle'] = $this->lang->line('payments') . ' ' . $this->lang->line('dashboard');
        $this->data['payments'] = $this->payments_m->payments();
        $this->session->set_userdata('page', 18);
        $this->tmadmin->tmView('payments/payments', $this->data);
    }
    
    public function getdatatableajax()
    {
    	$this->is_login(4);
    	 
    	$this->load->library('datatables');
    	 
    	$this->datatables
    	->select("payments.txn_id, payments.membership_name, users.username, payments.sender, payments.receiver_email, payments.payment_type, payments.paid_amount, payments.paid_date",FALSE)
    	->from("payments")
    	->join('users', 'payments.user=users.id', 'left');
    	
    	$this->datatables->edit_column('txn_id', '<span class="badge badge-important" style="font-size: 11px;">$1</span>', 'payments.txn_id');
    	//$this->datatables->edit_column('paid_amount', '<span class="badge badge-purple">$2</span>', 'payments.paid_amount');
    
    	echo $this->datatables->generate();
    }

    public function new_payment() {
        $this->session->set_userdata('page', 18);
        if ($this->input->post()) {
            if ($this->input->post('membership') == '1') {
                $this->session->set_flashdata('message', lang('free_package_error'));
                redirect('payments');
            }

            $user = $this->payments_m->get_user($_POST['user']);
            if (!$user) {
                $this->session->set_flashdata('message', lang('user_not_found'));
                redirect('payments');
            } else {
                $payable = $this->payments_m->get_payable($_POST['membership']);

                $this->load->helper('string');
                $txn_id = random_string('alnum', 16);

                $data = array(
                    'user' => $user,
                    'txn_id' => $txn_id,
                    'membership_p' => $this->input->post('membership'),
                    'membership_name' => $this->payments_m->membership_name($_POST['membership']),
                    'receiver_email' => $this->settings_m->get_by('key', 'paypal_email')->value,
                    'sender' => $this->input->post('sender'),
                    'payment_type' => 'Direct Payment',
                    'paid_amount' => $payable,
                    'month' => date('m')
                );

                $this->payments_m->insert($data);

                $this->_log_activity($this->lang->line('new_payment'));
                $package_info = $this->payments_m->package_info($this->input->post('membership')); //get package info
                $expiry = $this->payments_m->expiry($user); //return membership expiry date
                $exp = $expiry + ($package_info['valid_days'] * 86400);

                $this->db->where('user_id', $user);
                $this->db->set('membership_id', $this->input->post('membership'));
                $this->db->set('membership_expiry', $exp);
                $this->db->update('profiles');
                $this->session->set_flashdata('message', lang('record_successfully_created'));
                redirect('payments');
            }
        } else {
            $this->data['ptitle'] = lang('payments') . ' ' . lang('dashboard');
            $this->tmadmin->tmView('payments/new_payment', $this->data);
        }
    }

    function _log_activity($message) {
        $this->db->set('user', $this->session->userdata('user_id'));
        $this->db->set('message', $message);
        $this->db->insert('timeline');
        return true;
    }

    public function is_login($level = 2) {
        if (!$this->ion_auth->logged_in()) {
            //redirect them to the login page
            redirect('login', 'refresh');
        } elseif (!$this->ion_auth->is_admin($level)) {
            $this->load->helper('admin');
            redirect(site_url(), 'refresh');
        }
    }

    public function paginationCreate($baseUrl, $totalRow, $perPage) {
        $pageConfig = array(
            'base_url' => site_url() . $baseUrl,
            'total_rows' => $totalRow,
            'per_page' => $perPage,
            'num_links' => 3,
            'full_tag_open' => '<div class="pagination"><ul>',
            'full_tag_close' => '</ul></div>',
            'first_link' => false,
            'last_link' => false,
            'first_tag_open' => '<li>',
            'first_tag_close' => '</li>',
            'prev_link' => '<i class="icon-double-angle-left"></i> Previous',
            'prev_tag_open' => '<li class="prev">',
            'prev_tag_close' => '</li>',
            'next_link' => 'Next <i class="icon-double-angle-right"></i>',
            'next_tag_open' => '<li>',
            'next_tag_close' => '</li>',
            'last_tag_open' => '<li>',
            'last_tag_close' => '</li>',
            'cur_tag_open' => '<li class="active"><a href="#">',
            'cur_tag_close' => '</a></li>',
            'num_tag_open' => '<li>',
            'num_tag_close' => '</li>'
        );

        $this->pagination->initialize($pageConfig);
        return $this->pagination->create_links();
    }

}

/* End of file admin_payments.php */
/* Location: ./application/controllers/admin_payments.php */